package com.litchi.adapter;

import java.util.List;
import com.litchi.R;
import com.litchi.app.MyApplication;
import com.litchi.bean.Goods;
import com.litchi.utils.Constants;
import com.litchi.utils.NumberUtils;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GoodsHomeGridAdapter extends BaseAdapter {
	private Context mContext;
	private MyApplication mApplication;
	private List<Goods> mList;

	public GoodsHomeGridAdapter(Context mContext, List<Goods> mList) {
		this.mContext = mContext;
		mApplication = (MyApplication) mContext.getApplicationContext();
		this.mList = mList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View inflate = null;
		ViewHolder holder = null;
		if (convertView == null) {
			inflate = ((Activity) mContext).getLayoutInflater().inflate(R.layout.item_grid_goods, null);
			holder = new ViewHolder();
			holder.imgGoods = (ImageView) inflate.findViewById(R.id.img_icon);
			holder.tvGoodsName = (TextView) inflate.findViewById(R.id.tv_title);
			holder.tvGoodsPrice = (TextView) inflate.findViewById(R.id.tv_price);
			inflate.setTag(holder);
		} else {
			inflate = convertView;
			holder = (ViewHolder) inflate.getTag();
		}
		Goods info = mList.get(position);
		holder.tvGoodsName.setText(info.getGoodsName());
		holder.tvGoodsPrice.setText(NumberUtils.formatPrice(info.getGoodsPrice()));
		mApplication.imgLoader.displayImage(Constants.HTTP_FILE + mList.get(position).getGoodsImg().getUrl(), holder.imgGoods);
		return inflate;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		ImageView imgGoods;
		TextView tvGoodsName;
		TextView tvGoodsPrice;
	}

}
